﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication2.CLASS
{
  public class Customer
    {
        #region " & PropertyS & "
        private string _CustomerId;
        public string CustomerId
        {
            get { return _CustomerId; }
            set { _CustomerId = value; }
        }
        private string _CustomerFName;
        public string CustomerFName
        {
            get { return _CustomerFName; }
            set { _CustomerFName = value; }
        }
        private string _CustomerLName;
        public string CustomerLName
        {
            get { return _CustomerLName; }
            set { _CustomerLName = value; }
        }
        private string _CustomerRd;
        public string CustomerRd
        {
            get { return _CustomerRd; }
            set { _CustomerRd = value; }
        }
        private string _CustomerAddress;
        public string CustomerAddress
        {
            get { return _CustomerAddress; }
            set { _CustomerAddress = value; }
        }
        private string _CustomerPhone;
        public string CustomerPhone
        {
            get { return _CustomerPhone; }
            set { _CustomerPhone = value; }
        }
        private string _XTolologchId;
        public string XTolologchId
        {
            get { return _XTolologchId; }
            set { _XTolologchId = value; }
        }
      private string _CustomerPass;
        public string CustomerPass
        {
            get { return _CustomerPass; }
            set { _CustomerPass = value; }
        }
       
        #endregion
        public Customer()
        {
            //butsets

        }
        public Customer(string _CustomerId, string _CustomerFName, string _CustomerLName, string _CustomerRd,  string _CustomerAddress, string _CustomerPhone, string _XTolologchId, string _CustomerPass)
        {
            this.CustomerId = _CustomerId;
            this.CustomerFName = _CustomerFName;
            this.CustomerLName = _CustomerLName;
            this.CustomerRd = _CustomerRd;
            this.CustomerAddress = _CustomerAddress;
            this.CustomerPhone = _CustomerPhone;
            this.XTolologchId = _XTolologchId;
            this._CustomerPass = _CustomerPass;

        }
        public static Customer getbyRec(string _byCustomerId)
        {
            Customer x = new Customer();

            string qry1 = string.Format("select *from  Customers where CustomerId=('{0}')", _byCustomerId);
            DataTable table = DBTools.selecQryTmp(qry1);

            try
            {
                return new Customer(table.Rows[0]["CustomerId"].ToString(), table.Rows[0]["CustomerFirstName"].ToString(), table.Rows[0]["CustomerLastName"].ToString(), table.Rows[0]["CustomerRd"].ToString(), table.Rows[0]["CustomerAddress"].ToString(), table.Rows[0]["CustomerPhone"].ToString(), table.Rows[0]["XTolologchId"].ToString(), table.Rows[0]["CustomerPass"].ToString());
            }
            catch (Exception e)
            {
                //MsgBox(ex.Message, MsgBoxStyle.OkCancel, "account->getAccountByRecID")'
                MessageBox.Show(string.Format("{0} Exception caught.", e));

                return null;
            }



        }
        public static bool AddFunction(string _CustomerId, string _CustomerFName, string _CustomerLName, string _CustomerRd,  string _CustomerAddress, string _CustomerPhone, string _XTolologchId, string _CustomerPass)
        {

            //DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureCustomer";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = _CustomerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerFName", SqlDbType.NVarChar, 25)).Value = _CustomerFName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerLName", SqlDbType.NVarChar, 25)).Value = _CustomerLName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerRd", SqlDbType.NVarChar, 10)).Value = _CustomerRd;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerAddress", SqlDbType.NVarChar, 50)).Value = _CustomerAddress;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerPhone", SqlDbType.NVarChar, 8)).Value = _CustomerPhone;
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = _XTolologchId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerPass", SqlDbType.NVarChar, 16)).Value = _CustomerPass;



            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "ADD";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;


        }
        public static bool EditFunction(string _byCustomerId, string _byCustomerFName, string _byCustomerLName, string _byCustomerRd, string _byCustomerAddress, string _byCustomerPhone, string _byXTolologchId, string _byCustomerPass)
        {


           // DBTools._Conn.Open();
            DBTools._Comm.CommandText = "StoredProcedureCustomer";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = _byCustomerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerFName", SqlDbType.NVarChar, 25)).Value = _byCustomerFName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerLName", SqlDbType.NVarChar, 25)).Value = _byCustomerLName;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerRd", SqlDbType.NVarChar, 10)).Value = _byCustomerRd;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerAddress", SqlDbType.NVarChar, 50)).Value = _byCustomerAddress;
            DBTools._Comm.Parameters.Add(new SqlParameter("@ХCustomerPhone", SqlDbType.NVarChar, 8)).Value = _byCustomerPhone;
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = _byXTolologchId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerPass", SqlDbType.NVarChar, 16)).Value = _byCustomerPass;

            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "EDIT";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;



        }

        public static bool DelFunction(string _byCustomerId)
        {


            DBTools._Comm.CommandText = "StoredProcedureCustomer";
            DBTools._Comm.CommandType = CommandType.StoredProcedure;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerId", SqlDbType.NVarChar, 8)).Value = _byCustomerId;
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerFName", SqlDbType.NVarChar, 25)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerLName", SqlDbType.NVarChar, 25)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerRd", SqlDbType.NVarChar, 10)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerAddress", SqlDbType.NVarChar, 50)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerPhone", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@XTolologchId", SqlDbType.NVarChar, 8)).Value = "";
            DBTools._Comm.Parameters.Add(new SqlParameter("@CustomerPass", SqlDbType.NVarChar, 16)).Value = "";


            DBTools._Comm.Parameters.Add(new SqlParameter("@Otype", SqlDbType.NVarChar, 4)).Value = "DEL";
            SqlParameter prm = new SqlParameter("@returnValue", SqlDbType.TinyInt);
            DBTools._Comm.Parameters.Add(prm);
            prm.Direction = ParameterDirection.Output;
            string isOk;
            try
            {
                int rowcount = DBTools._Comm.ExecuteNonQuery();
                DBTools._Comm.Parameters.Clear();
                isOk = Convert.ToString(prm.Value);



                if (isOk == "1")
                {
                    return true;

                }
                else
                {
                    return false;

                }
            }


            catch (SqlException ex)
            {
                DBTools._Comm.Parameters.Clear();
                MessageBox.Show(ex.Message);


            }

            return false;



        }
    }
}
